package evil.wolf.excute.query;


import evil.wolf.db.DatabaseManager;
import evil.wolf.objects.ObjectCTNhapXuat;
import evil.wolf.objects.ObjectMaterial;
import evil.wolf.objects.ObjectNhapXuat;
import evil.wolf.objects.ObjectTonDau;


public class ExcuteQuery {

	DatabaseManager dbmanager;  

//	ObjectMaterial objectMaterial;
	String query;
	String oldValue;
//	public ExcuteQuery (){
//		
////		this.objectMaterial = objectMaterial;
////		oldValue = objectMaterial.getmah();
//	}
//	
	public boolean dothing(String sql){
		
		dbmanager = new DatabaseManager();
//		dbmanager.openConnection();
		if(dbmanager.excuteQuery(sql)){
			dbmanager.excuteQuery("commit");
			dbmanager.closeConnect();
			return true;
		}
		else
			return false;
	}
	

	
//---------------------------------------- M A T E R I A L -------------------------------------------------
	public String selectData() {
		String select = "SELECT * FROM HANGHOA ORDER BY man ASC"; //
		return select;
	}
		
	public boolean insertMaterial(ObjectMaterial objectMaterial){

		query = "INSERT INTO hanghoa VALUES ( '"
				+ objectMaterial.getmah() + "', '"
				+ objectMaterial.gettenh() + "', '"
				+ objectMaterial.getmanh() + "', '"
				+ objectMaterial.getdvt() + "', '"
				+ objectMaterial.getdg() + "', '"
				+ objectMaterial.getsl() + "')"
				;
		if(dothing(query))
			return true;
		else
			return false;
	}
	
	public boolean updateMaterial(ObjectMaterial objectMaterial, String valuesCondition){
			
		query = "UPDATE HANGHOA SET mah = '"
				+ objectMaterial.getmah() + "', tenh = '"
				+ objectMaterial.gettenh() + "', man = '" 
				+ objectMaterial.getmanh() + "', dvt = '"
				+ objectMaterial.getdvt() + "', sl = '"
				+ objectMaterial.getsl() + "', dg = '"
				+ objectMaterial.getdg() + "' "
				+ " WHERE mah = '" + objectMaterial.getmah() + "'"	;
		if(dothing(query))
			return true;
		else
			return false;
	}
	
	public boolean deleteMaterial(String valuesCondition){
		
		query = "DELETE FROM HANGHOA WHERE mah = '" + valuesCondition + "'"	;
		if(dothing(query))
			return true;
		else
			return false;
	}
	
	//---------------------------------------- C T N H A P X U A T -------------------------------------------------
	
	public String selectCTNhapXuat() {
		String select = "SELECT * FROM ctnhapxuat"; //
		return select;
	}
	
	public boolean insertCTNhapXuat(ObjectCTNhapXuat objectCTNhapXuat){

		query = "INSERT INTO CTNhapXuat VALUES ( '"
				+ objectCTNhapXuat.getsoct() + "', '"
				+ objectCTNhapXuat.getmak()  + "', '"
				+ objectCTNhapXuat.getmah() + "', '"
				+ objectCTNhapXuat.getsl() + "', '"
				+ objectCTNhapXuat.getdg() + "', '"
				+ objectCTNhapXuat.gettg() + "')"
				;
		if(dothing(query))
			return true;
		else
			return false;
	}
	
	public boolean updateCTNhapXuat(ObjectCTNhapXuat objectCTNhapXuat, String valuesCondition, String valuesCondition2, String valuesCondition3){
			
		query = "UPDATE CTNhapXuat SET soct = '"
				+ objectCTNhapXuat.getsoct() + "', mak = '"
				+ objectCTNhapXuat.getmak() + "', mah = '" 
				+ objectCTNhapXuat.getmah() + "', sl = '"
				+ objectCTNhapXuat.getsl() + "', dg = '"
				+ objectCTNhapXuat.getdg() + "', tg = '"
				+ objectCTNhapXuat.gettg() + "' "
				+ " WHERE soct = '" + objectCTNhapXuat.getsoct() + "' AND mak = '" + valuesCondition2 + "' AND mah = '" + valuesCondition3 + "'";
		if(dothing(query))
			return true;
		else
			return false;
	}
	
	public boolean deleteCTNhapXuat(String valuesCondition1, String valuesCondition2, String valuesCondition3){
		
		query = "DELETE FROM ctnhapxuat WHERE soct = '" + valuesCondition1 + "' AND mak = '" + valuesCondition2 + "' AND mah = '" + valuesCondition3 + "'";
		if(dothing(query))
			return true;
		else
			return false;
	}
	
	//---------------------------------------- T O N D A U -------------------------------------------------
	
		public String selectTonDau() {
			
			String select = "SELECT * FROM Tondau"; //
			return select;
		}
		
		public boolean insertTonDau(ObjectTonDau objectTonDau){

			query = "INSERT INTO TonDau VALUES ( '"
					+ objectTonDau.getmah() + "', '"
					+ objectTonDau.getmak()  + "', '"
					+ objectTonDau.getsl() + "', '"
					+ objectTonDau.getdg() + "', '"
					+ objectTonDau.gettg() + "')"
					;
			if(dothing(query))
				return true;
			else
				return false;
		}
		
		public boolean updateTonDau(ObjectTonDau objectTonDau, String valuesCondition1, String valuesCondition2 ){
				
			query = "UPDATE TonDau SET  mah = '"
					+ objectTonDau.getmah() + "', mak = '"
					+ objectTonDau.getmak()	+ "', sl = '"
					+ objectTonDau.getsl() + "', dg = '"
					+ objectTonDau.getdg() + "', tg = '"
					+ objectTonDau.gettg() + "' "
					+ " WHERE mah = '" + objectTonDau.getmah() + "' AND mak = '" + objectTonDau.getmak() + "'"	;
			if(dothing(query))
				return true;
			else
				return false;
		}
		
		public boolean deleteTonDau(String valuesCondition1, String valuesCondition2){
			
			query = "DELETE FROM TonDau WHERE  mah = '" + valuesCondition1 + "' AND mak = '"+ valuesCondition2 +"'";
			if(dothing(query))
				return true;
			else
				return false;
		}
		
		//---------------------------------------- N H A P -------------------------------------------------
		
			public String selectNhap() {
				String select = "SELECT soct, ngayct, tenkh " +
						"FROM NhapXuat, KhachHang " +
						"WHERE NhapXuat.loaiCT = 'N' AND NhapXuat.makh = KhachHang.makh"; //
				return select;
			}
			
			public boolean insertNhap(ObjectNhapXuat objectNhapXuat){

				query = "INSERT INTO NhapXuat VALUES ( '"
						+ objectNhapXuat.getsoct() + "', to_date('"
						+ objectNhapXuat.getngayct()  + "', 'dd/MM/yyyy'), '"
						+ objectNhapXuat.getmakh() + "', '"
						+ objectNhapXuat.getdg() + "', '"
						+ objectNhapXuat.getloaict() + "')"
						;
				if(dothing(query))
					return true;
				else
					
					return false;
			}
			
			public boolean updateNhap(ObjectNhapXuat objectNhapXuat,  String valuesCondition1, String valuesCondition2, String valuesCondition3){
					
				query = "UPDATE NhapXuat SET soct = '"
						+ objectNhapXuat.getsoct() + "', ngayct = to_date ('"
						+ objectNhapXuat.getngayct() + "', 'dd/MM/yyyy'), makh = '"
						+ objectNhapXuat.getmakh() + "', dg = '"
						+ objectNhapXuat.getdg() + "', loaict = '"
						+ objectNhapXuat.getloaict() + "' "
						+ " WHERE soct = '" + valuesCondition1 + "' AND makh = '" + valuesCondition2 + "' AND loaict = '" + valuesCondition3 + "'";
				if(dothing(query))
					return true;
				else
					return false;
			}
			
			public boolean deleteNhap(String valuesoct, String valuemakh, String valuesloaict){
				
				query = "DELETE FROM NhapXuat WHERE  soct = '" + valuesoct + "' AND makh = '" + valuemakh 
																					+ "' AND loaict = '" + valuesloaict + "'";
				if(dothing(query))
					return true;
				else
					return false;
			}

}